package com.zrrd.conversionFlow;

import java.io.*;

/**
 * @author WanJl
 * @version 1.0
 * @title ConversionFlowDemo01
 * @description 转换流
 *
 * |-基本流：特点：直接操作文件
 *      |-字节流：InputStream OutputStream
 *      |-字符流：Reader Writer
 * |-处理流：特点：操作的对象是流，操作的不是文件
 *      |-缓冲流：
 *          |-字节缓冲流：BufferedOutputStream BufferedInputStream
 *          |-字符缓冲流：BufferedReader BufferedReader
 *      |-转换流：InputStreamReader OutputStreamWriter
 *
 *
 * InputStreamReader:字节流--->字符流
 * OutputStreamWriter: 字符流--->字节流
 *
 * @create 2024/5/26
 */
public class ConversionFlowDemo01 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        InputStreamReader isr = null;
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        try {
            //创建文件字节输入流
            fis=new FileInputStream("E:/users/哈哈哈哈.txt");
            isr=new InputStreamReader(fis,"GBK");//创建转换流

            fos=new FileOutputStream("D:/users/哈哈哈哈.txt");
            osw=new OutputStreamWriter(fos,"UTF-8");

            //输入
            char[] buf=new char[1024];
            int len;
            while ((len=isr.read(buf))!=-1){
                osw.write(buf, 0, len);
            }


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                osw.close();
                fos.close();
                isr.close();
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
